package com.Sdebruijn.curriculumvitae.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.Sdebruijn.curriculumvitae.R;
import com.Sdebruijn.curriculumvitae.asynctask.DownloadPersonDataAsyncTask;
import com.Sdebruijn.curriculumvitae.asynctask.LoadPersonDataAsyncTask;
import com.Sdebruijn.curriculumvitae.models.Applicant;
import com.Sdebruijn.curriculumvitae.util.ViewUtils;
import com.Sdebruijn.curriculumvitae.view.ProgressWheel;
import com.Sdebruijn.curriculumvitae.view.RemoteImageView;
import com.actionbarsherlock.app.SherlockFragment;

/**
 * Fragment showing the applicant's personal and online data
 * 
 * @author Stefan de Bruijn
 * 
 */
public class PersonFragment extends SherlockFragment {

	/**
	 * Key used to fetch the Applicant object from the Bundle savedInstanceState
	 */
	private static final String KEY_APPLICANT = "com.Sdebruijn.cirriculumvitae.PersonFragment.applicant";

	/**
	 * Applicant object is kept in order to maintain our state
	 */
	public static Applicant mApplicant;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_person, null);

		// If we have a former state we restore it, else (down)load the data
		if (savedInstanceState != null) {
			refreshFromInstanceState(view, savedInstanceState);
		} else {
			DownloadPersonDataAsyncTask mDownloadPersonDataAsyncTask = new DownloadPersonDataAsyncTask(
					view, this.getActivity());
			mDownloadPersonDataAsyncTask.execute();
		}

		return view;
	}

	/**
	 * Reloads all data after this Fragment gets restored, from the Bundle
	 * savedInstanceState
	 * 
	 * @param view
	 *            The fragment's general view
	 * @param savedInstanceState
	 *            Bundle with our saved former state
	 */
	private void refreshFromInstanceState(View view, Bundle savedInstanceState) {

		// Fetch our Applicant from the Bundle
		mApplicant = savedInstanceState.getParcelable(KEY_APPLICANT);

		// If re-marshaling is successful, load the Applicant data
		if (mApplicant != null) {

			// Initiate the views
			LinearLayout ll_person_data = (LinearLayout) view.findViewById(R.id.ll_person_data);
			ProgressWheel pg = (ProgressWheel) view.findViewById(R.id.pw_spinner);

			RemoteImageView picture = (RemoteImageView) view.findViewById(R.id.riv_picture);
			TextView name = (TextView) view.findViewById(R.id.tv_name);
			TextView title = (TextView) view.findViewById(R.id.tv_title);
			TextView company = (TextView) view.findViewById(R.id.tv_company);
			TextView email = (TextView) view.findViewById(R.id.tv_email);
			TextView nationality = (TextView) view.findViewById(R.id.tv_nationality);
			TextView dob = (TextView) view.findViewById(R.id.tv_dob);
			TextView mobile = (TextView) view.findViewById(R.id.tv_mobile);
			TextView tv_linkedin = (TextView) view.findViewById(R.id.tv_linkedin);
			TextView tv_google_plus = (TextView) view.findViewById(R.id.tv_google_plus);

			// Fill the views with data again
			picture.setRemotePath(mApplicant.picture);
			picture.loadImage(true);
			name.setText(mApplicant.name);
			title.setText(mApplicant.current_title);
			company.setText(mApplicant.current_institution);
			email.setText(mApplicant.email);
			nationality.setText(mApplicant.nationality);
			dob.setText(mApplicant.date_of_birth);
			mobile.setText(mApplicant.mobile);
			tv_linkedin.setText(mApplicant.online_person.linkedin_url);
			tv_google_plus.setText(mApplicant.online_person.google_plus_url);

			// Switch visibility of our LinearLayout holding all the data and
			// the loading-indicator
			ViewUtils.switchVisibleState(ll_person_data, ViewUtils.State.VISIBLE, pg,
					ViewUtils.State.INVISIBLE);

			// If re-marshaling fails, load Applicant from the database
		} else {
			LoadPersonDataAsyncTask mLoadPersonDataAsyncTask = new LoadPersonDataAsyncTask(view,
					this.getActivity());
			mLoadPersonDataAsyncTask.execute();
		}
	}

	@Override
	public void onSaveInstanceState(Bundle savedInstanceState) {
		super.onSaveInstanceState(savedInstanceState);
		// Save our Applicant object so we can restore it later
		savedInstanceState.putParcelable(KEY_APPLICANT, mApplicant);
	}
}
